package leetcode.string;

import java.util.HashMap;

/**
 * @author mazouri
 * @create 2021-09-26 23:20
 */
public class _3_lengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) return 0;
        HashMap<Character, Integer> map = new HashMap<>();
        char[] c = s.toCharArray();
        int res = 1;
        int left = 0;
        int right = 1;
        map.put(c[left], left);
        while (left < right && right < s.length()) {
            if (map.containsKey(c[right])) {
                left = Math.max(left, map.get(c[right]) + 1);
            }
            map.put(c[right], right);
            res = Math.max(res, right - left + 1);
            right++;
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new _3_lengthOfLongestSubstring().lengthOfLongestSubstring("abcbabcbb"));
    }
}